package flc.ast.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.listener.OnPageChangeListener;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.SelPicActivity;
import flc.ast.adapter.HomeIndicatorAdapter;
import flc.ast.adapter.MyBannerAdapter;
import flc.ast.adapter.SpliceAdapter;
import flc.ast.bean.MyBannerBean;
import flc.ast.databinding.FragmentHomeBinding;
import flc.ast.utils.Content;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private SpliceAdapter spliceAdapter;
    private HomeIndicatorAdapter indicatorAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);
        mDataBinding.rvSpliceList.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL,false));
        spliceAdapter=new SpliceAdapter();
        mDataBinding.rvSpliceList.setAdapter(spliceAdapter);
        spliceAdapter.setOnItemClickListener(this);
        List<MyBannerBean> listBanner = new ArrayList<>();
        listBanner.add(new MyBannerBean(R.drawable.fangxing1,getString(R.string.type_text1)));
        listBanner.add(new MyBannerBean(R.drawable.shangxia1,getString(R.string.type_text2)));
        listBanner.add(new MyBannerBean(R.drawable.zuoyou1,getString(R.string.type_text3)));
        mDataBinding.rvIndicatorList.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL,false));
        indicatorAdapter=new HomeIndicatorAdapter();
        indicatorAdapter.setList(listBanner);
        mDataBinding.rvIndicatorList.setAdapter(indicatorAdapter);
        MyBannerAdapter bannerAdapter = new MyBannerAdapter(listBanner);
        mDataBinding.homeBanner.setAdapter(bannerAdapter, true);//false不无限循环
        mDataBinding.homeBanner.isAutoLoop(true);//false不自动轮播
        //点击监听
        mDataBinding.homeBanner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(Object data, int position) {
                if (position==0){
                    gotoSelPic(Content.Split_Zy_Fx);
                }else if (position==1){
                    gotoSelPic(Content.Split_Zy_Sx);
                }else {
                    gotoSelPic(Content.Split_Zy_Zy);
                }
            }
        });
        mDataBinding.homeBanner.addOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                indicatorAdapter.setSelPosition(position);
                indicatorAdapter.notifyDataSetChanged();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (position==0){
            gotoSelPic(Content.Split_Shu);
        }else if (position==1){
            gotoSelPic(Content.Split_Heng);
        }else if (position==2){
            gotoSelPic(Content.Split_Zy);
        }
    }

    @Override
    protected void initData() {
        List<Integer> list=new ArrayList<>();
        list.add(R.drawable.shutu1);
        list.add(R.drawable.hengtu1);
        list.add(R.drawable.ziyou1);
        spliceAdapter.setList(list);
    }

    //去到选择图片界面
    private void gotoSelPic(String flag) {
        Intent intent = new Intent(mContext, SelPicActivity.class);
        intent.putExtra(Content.Flag, flag);
        startActivity(intent);
    }

}